{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "import math\n",
    "import time\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_size = 32\n",
    "num_batches = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_activations(t):\n",
    "    print(t.op.name , ' ', t.get_shape().as_list())\n",
    "\n",
    "def inference(images):\n",
    "    parameters = []\n",
    "    with tf.name_scope('conv1') as scope :\n",
    "        kernel = tf.Variable(tf.truncated_normal([11,11,3,64], dtype= tf.float32 , stddev= 0.1), name = 'weights')\n",
    "        conv = tf.nn.conv2d(images , kernel , [1,4,4,1] ,padding = 'SAME')\n",
    "        biases = tf.Variable(tf.constant(0.0, shape= [64], dtype= tf.float32) , trainable= True , name= 'biases')\n",
    "        bias = tf.nn.bias_add(conv, biases)\n",
    "        conv1 = tf.nn.relu(bias, name= scope)\n",
    "        print_activations(conv1)\n",
    "        parameters += [kernel , biases]\n",
    "\n",
    "    lrn1 = tf.nn.lrn(conv1, 4, bias =1.0 , alpha = 0.001/9, beta=0.75, name = 'lrn1')\n",
    "    pool1 = tf.nn.max_pool(lrn1, ksize= [1,3,3,1], strides=[1,2,2,1], padding = 'VALID', name = 'pool1')\n",
    "    print_activations(pool1)\n",
    "    \n",
    "    with tf.name_scope('conv2') as scope:\n",
    "        kernel = tf.Variable(tf.truncated_normal([5, 5, 64, 192], dtype=tf.float32, stddev=0.1), name='weights')\n",
    "        conv = tf.nn.conv2d(pool1, kernel, [1, 1, 1, 1], padding='SAME')\n",
    "        biases = tf.Variable(tf.constant(0.0, shape=[192], dtype=tf.float32), trainable=True, name='biases')\n",
    "        bias = tf.nn.bias_add(conv, biases)\n",
    "        conv2 = tf.nn.relu(bias, name=scope)\n",
    "        print_activations(conv2)\n",
    "        parameters += [kernel, biases]\n",
    "\n",
    "    lrn2 = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.001 / 9, beta=0.75, name='lrn2')\n",
    "    pool2 = tf.nn.max_pool(lrn2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool2')\n",
    "    print_activations(pool2)\n",
    "\n",
    "    with tf.name_scope('conv3') as scope:\n",
    "        kernel = tf.Variable(tf.truncated_normal([3, 3, 192, 384], dtype=tf.float32, stddev=0.1), name='weights')\n",
    "        conv = tf.nn.conv2d(pool2, kernel, [1, 1, 1, 1], padding='SAME')\n",
    "        biases = tf.Variable(tf.constant(0.0, shape=[384], dtype=tf.float32), trainable=True, name='biases')\n",
    "        bias = tf.nn.bias_add(conv, biases)\n",
    "        conv3 = tf.nn.relu(bias, name=scope)\n",
    "        print_activations(conv3)\n",
    "        parameters += [kernel, biases]\n",
    "\n",
    "    with tf.name_scope('conv4') as scope:\n",
    "        kernel = tf.Variable(tf.truncated_normal([3, 3, 384, 256], dtype=tf.float32, stddev=0.1), name='weights')\n",
    "        conv = tf.nn.conv2d(conv3, kernel, [1, 1, 1, 1], padding='SAME')\n",
    "        biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), trainable=True, name='biases')\n",
    "        bias = tf.nn.bias_add(conv, biases)\n",
    "        conv4 = tf.nn.relu(bias, name=scope)\n",
    "        print_activations(conv4)\n",
    "        parameters += [kernel, biases]\n",
    "\n",
    "    with tf.name_scope('conv5') as scope:\n",
    "        kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], dtype=tf.float32, stddev=0.1), name='weights')\n",
    "        conv = tf.nn.conv2d(conv4, kernel, [1, 1, 1, 1], padding='SAME')\n",
    "        biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), trainable=True, name='biases')\n",
    "        bias = tf.nn.bias_add(conv, biases)\n",
    "        conv5 = tf.nn.relu(bias, name=scope)\n",
    "        print_activations(conv5)\n",
    "        parameters += [kernel, biases]\n",
    "\n",
    "    pool5 = tf.nn.max_pool(conv5,ksize=[1,3,3,1], strides= [1,2,2,1], padding= 'VALID', name='pool5')\n",
    "    print_activations(pool5)\n",
    "    return pool5,parameters\n",
    "\n",
    "def time_tensorflow_run(session, target, info_string):\n",
    "    num_steps_burn_in = 10\n",
    "    total_duration = 0.0\n",
    "    total_duration_squared = 0.0\n",
    "    for i in range(num_batches * num_steps_burn_in):\n",
    "        start_time = time.time()\n",
    "        _ = session.run(target)\n",
    "        duration = time.time() - start_time\n",
    "        if i >= num_steps_burn_in:\n",
    "            if not i % 10:\n",
    "                print('%s:step %d ,duration = %.3f'%(datetime.now(),i - num_steps_burn_in,duration))\n",
    "            total_duration += duration\n",
    "            total_duration_squared +=duration * duration\n",
    "\n",
    "    mn = total_duration /num_batches\n",
    "    vr = total_duration_squared / num_batches + mn * mn\n",
    "    sd = math.sqrt(vr)\n",
    "    print('%s: %s across %d steps , %.3f +/- %.3f sec /batch'%(datetime.now(), info_string, num_batches, mn , sd))\n",
    "    \n",
    "def run_benchmark():\n",
    "    with tf.Graph().as_default():\n",
    "        image_size = 224\n",
    "        images = tf.Variable(tf.random_normal([batch_size,image_size,image_size,3], dtype= tf.float32, stddev= 0.1))\n",
    "        pool5 ,parameters = inference(images)\n",
    "        init = tf.global_variables_initializer()\n",
    "        sess = tf.Session()\n",
    "        sess.run(init)\n",
    "        #time_tensorflow_run(sess, pool5, 'Forward')\n",
    "        objective = tf.nn.l2_loss(pool5)\n",
    "        grad = tf.gradients(objective, parameters)\n",
    "        #time_tensorflow_run(sess, grad , 'Forward-Backward')\n",
    "          writer = tf.summary.FileWriter('/home/a503wangsiqi/tensorboard/alexnet_log',tf.get_default_graph())\n",
    "        writer.close()\n",
    "        \n",
    "def run_benchmark():\n",
    "    with tf.Graph().as_default():\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conv1   [32, 56, 56, 64]\n",
      "pool1   [32, 27, 27, 64]\n",
      "conv2   [32, 27, 27, 192]\n",
      "pool2   [32, 13, 13, 192]\n",
      "conv3   [32, 13, 13, 384]\n",
      "conv4   [32, 13, 13, 256]\n",
      "conv5   [32, 13, 13, 256]\n",
      "pool5   [32, 6, 6, 256]\n"
     ]
    }
   ],
   "source": [
    "run_benchmark()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
